import 'package:enterprise_v6_app/app/data/api/special_list.dart';
import 'package:enterprise_v6_app/app/data/common/charts_pie.dart';
import 'package:enterprise_v6_app/app/data/entities/application/special_list/work_count_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/special_list/work_history_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/special_list/work_today_status_entity.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

class GeneralViewAllController extends GetxController {
  /// 当前作业(任务板) 数据
  WorkCountEntity currentJob = WorkCountEntity();

  /// 作业单位违章概况 数据
  WorkTodayStatusEntity unitViolation = WorkTodayStatusEntity();

  ///当前作业(任务板) 作业总数
  int taskTotal = 0;

  /// 任务版数据
  List<ChartData> questionData = [
    // ChartData('未签发', 36, const Color(0xFFFFCE24)),
    // ChartData('已签发', 24, const Color(0xFF649FFF)),
    // ChartData('已验收', 19, const Color(0xFF14D2B8)),
    // ChartData('作废', 21, const Color(0xFFFD6B6B)),
    // ChartData('撤销', 21, const Color(0xFF6788CF)),
  ];

  ///作业异常数据
  List<WorkHistoryEntity> workHistoryList = [];

  ///历史概述总数
  String historyTotal = '';

  /// 作业历史概述颜色
  List<Color> specialColorList = [
    const Color(0xffFD6B6B),
    const Color(0xffF7BA1E),
    const Color(0xffFC9900),
    const Color(0xff14C9C9),
    const Color(0xff2C6CFF),
    const Color(0xffB17AFF),
    const Color(0xffFD6B6B),
    const Color(0xff3491FA),
  ];

  // 特殊作业数据
  List stringData = [];

  @override
  void onInit() {
    super.onInit();
    //  获取当前作业数据
    _getWorkCount();
    //  作业单位违章概况
    _getWorkViolation();
    //  获取任务板
    _getTaskBoard();
    //  作业异常
    _getTicketReceiptValidDate();
    // 获取特殊作业历史概览
    _getSpecialWorkHistory();
  }

  /// 作业中数量及违章作业数量统计
  _getWorkCount() async {
    final res = await SpecialListApis.getWorkCount().execute();
    res.onSuccess((result) {
      if (result.data != null) {
        currentJob = result.data!;
      }
      update();
    });
  }

  /// 作业单位违章概况
  _getWorkViolation() async {
    final res = await SpecialListApis.getWorkViolation().execute();
    res.onSuccess((result) {
      if (result.data != null) {
        unitViolation = result.data!;
      }
      update();
    });
  }

  /// 获取任务板(作业状态数量统计)
  _getTaskBoard() async {
    final res = await SpecialListApis.getTaskBoard(0).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        final WorkCountEntity data = result.data!;
        //添加数据
        questionData = [
          ChartData(
              '未签发', data.notIssuedCount!.toDouble(), const Color(0xFFFFCE24)),
          ChartData(
              '已签发', data.issuedCount!.toDouble(), const Color(0xFF649FFF)),
          ChartData(
              '已验收', data.acceptedCount!.toDouble(), const Color(0xFF14D2B8)),
          ChartData(
              '作废', data.invalidCount!.toDouble(), const Color(0xFFFD6B6B)),
          ChartData(
              '撤销', data.revokeCount!.toDouble(), const Color(0xFF6788CF)),
        ];
        //任务板总数
        taskTotal = data.notIssuedCount! +
            data.issuedCount! +
            data.acceptedCount! +
            data.invalidCount! +
            data.revokeCount!;
      }
      update();
    });
  }

  /// 获取作业类型异常数量统计
  _getTicketReceiptValidDate() async {
    final res = await SpecialListApis.getWorkTypeException(0).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        workHistoryList = result.data!;
        int total = 0;
        // 获取作业总数
        for (var item in workHistoryList) {
          total += item.ticketCount!;
        }
        // 如果总数不为空
        if (total != 0) {
          // 填充页面作业异常数据
          for (var item in workHistoryList) {
            item.value = item.ticketCount! / total;
          }
        }
        update();
      }
    });
  }

  /// 获取特殊作业历史概览
  _getSpecialWorkHistory() async {
    final res = await SpecialListApis.getSpecialWorkHistory(1).execute();
    res.onSuccess((result) {
      if (result.data != null && result.data!.isNotEmpty) {
        final Map<String, dynamic> map = result.data!;
        final List<WorkHistoryEntity> workHistoryList = [];
        //历史概述总数
        historyTotal = map.keys.first;
        //历史概述值
        final List workList = map.values.first as List;
        //添加数据
        for (var item in workList) {
          workHistoryList
              .add(WorkHistoryEntity.fromJson(item as Map<String, dynamic>));
        }
        //填充页面数据
        for (int i = 0; i < workHistoryList.length; i++) {
          final item = workHistoryList[i];
          stringData.add(
            {
              'name': item.ticketTypeName,
              'color': specialColorList[i],
              'num': item.ticketCount!
            },
          );
        }
      }
    });
  }
}
